<?php

include("config.php");
if (!isset($_SESSION["user"]) || $_SESSION["user"] == "" || $kreditas != "1") header('Location: index.php');
include("header.php");
include("menu.php");

$page_size = "500";

function print_pages2($page,$page_size,$record_count,$cards){
	$cnt=1;
	$first_card="";
	$last_card="";
	$p = 1;
	if ($record_count > $page_size) {
		foreach ($cards as $card) {
			if ($first_card == "") {
				$first_card = trim($card["N51_PAV"]);
			}
			$last_card=trim($card["N51_PAV"]);
			if ($cnt >= $page_size) {
				if ($page == $p) {
					$cur="b";
				} else {
					$cur="";
				}
				$cnt = 0;
				echo "<A href='cards.php?p=".$p."' class='card'><span class='h20".$cur."'>" .$first_card . " - " . $last_card . "</span></A><br>";
				$first_card="";
				$p++;
			}
			$cnt++;
		}
		if ($cnt != "1") {
			if ($page == $p) {
				$cur="b";
			} else {
				$cur="";
			}		
			echo "<A href='cards.php?p=".$p."' class='card'><span class='h20".$cur."'>" .$first_card . " - " . $last_card . "</span></A><br>";
		}
	}
}

if(!isset($_REQUEST['p']) || !is_numeric($_REQUEST['p'])){
	$page = 1;
} else {
	$page = $_REQUEST['p'];
}
$db_www = new DB($server_username,$server_pass,$server_db,$server,$driver);
if ($db_www->isConnected()) {	
	$i_query = str_replace("<kodas>", $_SESSION['user'], $i_query);
	$minfo = $db_www->returnInfo($i_query);	

	
} else {
	die("Klientų aptarnavimo sistema laikinai neveikia.");
}

if (isset($_REQUEST["act"])){
	if ($_REQUEST["act"] == "change" && isset($_REQUEST["descr"]) && isset($_REQUEST["nr"])) {
		$kchange_query = str_replace("<kodas>", $minfo["N08_kodas_ks"], $kchange_query);
		$kchange_query = str_replace("<pastabos>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["descr"])), $kchange_query);
		$kchange_query = str_replace("<kortele>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["nr"])), $kchange_query);
		$db_www->execute($kchange_query);
		logAction("Klientas koreguoja korteles duomenis:" . $_REQUEST["nr"],3);
  } else if ($_REQUEST["act"] == "block" && isset($_REQUEST["nr"])) {
               
         $kblock_query = str_replace("<kodas>", $minfo["N08_kodas_ks"], $kblock_query);
         $kblock_query = str_replace("<block>", "BLOCK", $kblock_query);
         $kblock_query = str_replace("<kortele>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["nr"])), $kblock_query);
         $db_www->execute($kblock_query);
         logAction("Klientas užblokuoja kortelę:" . $_REQUEST["nr"],2);
  } else if ($_REQUEST["act"] == "unblock" && isset($_REQUEST["nr"])) {
               
         $kblock_query = str_replace("<kodas>", $minfo["N08_kodas_ks"], $kblock_query);
         $kblock_query = str_replace("<block>", "UNBLOCK", $kblock_query);
         $kblock_query = str_replace("<kortele>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["nr"])), $kblock_query);
               
         $db_www->execute($kblock_query);
         logAction("Klientas atblokuoja kortelę:" . $_REQUEST["nr"],2);  
  }

}	
	if (!empty($minfo)) {
		$k_query = str_replace("<kodas>", $minfo["N08_kodas_ks"], $k_query);
		$cards = $db_www->returnList($k_query);	
		//echo $k_query;
	} else {
		die("Nerasta vartotojo įmonė.");
	}

?>
<!-- IE specific code: -->
<!--[if lte IE 9]> 
<style type="text/css"> 
#aep_ovrl { 
position: absolute; 
filter:alpha(opacity=70); 
top: expression(eval(document.body.scrollTop)); 
width: expression(eval(document.body.clientWidth)); 
} 
#aep_ww {  
position: absolute;  
top: expression(eval(document.body.scrollTop));  
} 
</style> 
<![endif]-->
<!-- ae_prompt HTML code -->
<div id="aep_ovrl" style="display: none;"></div>
<div id="aep_ww" style="display: none;">
<div id="aep_win"><div id="aep_t"></div>
<div id="aep_w"><div id="aep_center"><span id="aep_prompt"></span>
<br /><br />Pavadinimas: <input type="text" id="aep_text" onKeyPress=
"if((event.keyCode==10)||(event.keyCode==13)) ae_clk(1); if (event.keyCode==27) ae_clk(0);">
<br />PIN: <input MAXLENGTH=4 type="text" id="aep_pin" onKeyPress=
"if((event.keyCode==10)||(event.keyCode==13)) ae_clk(1); if (event.keyCode==27) ae_clk(0);">
<br><div><input class="but bgfilter" type="button" id="aep_ok" onclick="ae_clk(1);" value="Gerai">
<input class="but bgfilter" type="button" id="aep_cancel" onclick="ae_clk(0);" value="Atšaukti">
</div></div>
</div>
</div>
</div>
<!-- ae_prompt HTML code -->

<SCRIPT language="JavaScript">
<!--
var card = null;
function changeDesc(nr,val) {
    card = nr;
	idx = val.indexOf(";");
	if (idx >= 0) {
		v1 = val.substring(0,idx);
		v2 = val.substring(idx+1);
	} else {
		v1 = "";
		v2 = val;
	}
	ae_prompt( hw2, 'Įveskite kortelės pavadinimą ir PIN kodą', v1,v2);
}

 
function hw2(n)
{
    if (n != null)
		window.location='cards.php?act=change&nr='+card +'&descr='+n;
}

// This is variable for storing callback function 
var ae_cb = null;

 
// this is a simple function-shortcut 
// to avoid using lengthy document.getElementById 
function ae$(a) { return document.getElementById(a); } 
 
// This is a main ae_prompt function 
// it saves function callback  
// and sets up dialog 
function ae_prompt(cb, q, b,a) { 
	ae_cb = cb;
	ae$('aep_t').innerHTML = document.domain + ' klausimas:';
	ae$('aep_prompt').innerHTML = q;
	ae$('aep_text').value = a;
	ae$('aep_pin').value = b;
	ae$('aep_ovrl').style.display = ae$('aep_ww').style.display = '';
	ae$('aep_text').focus();
	ae$('aep_text').select();
} 
 
// This function is called when user presses OK(m=0) or Cancel(m=1) button 
// in the dialog. You should not call this function directly. 
function ae_clk(m) { 
	if (!m)  
		ae_cb(null);  // user pressed cancel, call callback with null 
	else {
	    pin = ae$('aep_pin').value;
		if (!isUnsignedInteger(pin)) {
			alert("PIN kodas turi būti skaičius");
			return;
		}
		if (pin.length == 0) pin = "0000";
		else if (pin.length == 1) pin = "000" + pin;
		else if (pin.length == 2) pin = "00" + pin;
		else if (pin.length == 3) pin = "0" + pin;
		ae_cb(pin + ";" + ae$('aep_text').value); // user pressed OK  
	}	
	// hide dialog layers  
	ae$('aep_ovrl').style.display = ae$('aep_ww').style.display = 'none';
}

function isUnsignedInteger(s) {
  return (s.toString().search(/^[0-9]+$/) == 0);
}

function status(n,nr)
{
    if (n == "1") {
		var answer = confirm("Ar blokuoti kortelę?")
		if (answer){
			window.location='cards.php?act=block&nr='+nr ;
		}
	} else {
		var answer = confirm("Ar atblokuoti kortelę?")
		if (answer){
			window.location='cards.php?act=unblock&nr='+nr ;
		}
	}	
}

//-->
</SCRIPT>
<table class="innercont" cellspacing="0" cellpadding="5">
<?php
if (isset($oldstyle) && $oldstyle == "1") {
?>
<tr><td class="title">Kortelės</td></tr>
<tr><td>
<?php
} else {
	echo "<tr><td class='leftcol'><br>";
	print_pages2($page,$page_size,count($cards),$cards);
	echo "</td><td class='rightcol'>";
}
?>

<?php
if (!empty($cards)) {
?>
<table width="100%" border = "0">
<tr>
<td valign="top">
<?php
if (isset($oldstyle) && $oldstyle == "1") {
	print_pages2($page,$page_size,count($cards),$cards);
}
?>
</td>
<td align="center" valign="top"><br>
<table cellspacing="0" cellpadding="5" class="innercont3 width100pr">
<?php

$cnt=0;
$min = ($page-1) * $page_size;
$max = $page * $page_size;
if (!isset($oldstyle) || $oldstyle != "1") {
	echo "<tr><th nowrap>Kortelės nr.</th><th>Pavadinimas</th><th>&nbsp;</th><th colspan='2'>Blokuoti</th></tr>";
	foreach ($cards as $card) {
		$cnt++;
		if ($cnt > $min && $cnt <= $max) {
			echo "<tr>";
			echo "<td>".trim($card["N51_PAV"])."</td>";
			echo "<td>".@iconv('windows-1257', 'UTF-8', htmlspecialchars($card["N51_KORTA"])). " " .@iconv('windows-1257', 'UTF-8', htmlspecialchars($card["n35_pav"])) ."</td>";
			echo "<td><A class='card2' href='#' onclick='changeDesc(\"".@iconv('windows-1257', 'UTF-8', trim($card["N51_KODAS_KT"]))."\",\"".@iconv('windows-1257', 'UTF-8', str_replace("'", '\\"',str_replace('"', '\"', trim($card["N51_PASTABOS"]))))."\")'>Koreguoti</A>";
			echo "/<A href='card.php?code=".@iconv('windows-1257', 'UTF-8', trim($card["N51_PAV"]))."' class='card2'>Istorija</A></td>";
			echo "<td>";
			if ($card["N51_VISKAS"] == "0") {
				echo "<A onclick=\"status('1','".@iconv('windows-1257', 'UTF-8', trim($card["N51_KODAS_KT"]))."');\"><img src='img/green_new.gif' alt='' title=''></A>";
			} else {
				echo "<A onclick=\"status('0','".@iconv('windows-1257', 'UTF-8', trim($card["N51_KODAS_KT"]))."');\"><img src='img/red_new.gif' alt='' title=''></A>";
			}
			echo "</td><td></td></tr>\n";
		}
	}	
	echo "<tr><td colspan='5' style='text-align:right;'><img src='img/green_new.gif' alt='' title=''> - Aktyvi kortelė <img src='img/red_new.gif' alt='' title=''> - Blokuota kortelė</td></tr>";
} else {
	foreach ($cards as $card) {
		$cnt++;
		if ($cnt > $min && $cnt <= $max) {
			echo "<tr><td align='right' width='130'>";
			if ($card["N51_VISKAS"] == "0") {
				echo "<A onclick=\"status('1','".@iconv('windows-1257', 'UTF-8', trim($card["N51_KODAS_KT"]))."');\"><img src='img/green.gif' alt='' title=''></A>";
			} else {
				echo "<A onclick=\"status('0','".@iconv('windows-1257', 'UTF-8', trim($card["N51_KODAS_KT"]))."');\"><img src='img/red.gif' alt='' title=''></A>";
			}
			echo "</td><td width='120'><A href='card.php?code=".@iconv('windows-1257', 'UTF-8', trim($card["N51_PAV"]))."' class='card'>".trim($card["N51_PAV"])."</A></td><td width='300'>".@iconv('windows-1257', 'UTF-8', htmlspecialchars($card["N51_KORTA"])). " " .@iconv('windows-1257', 'UTF-8', htmlspecialchars($card["n35_pav"])) ."</td><td><button onclick='changeDesc(\"".@iconv('windows-1257', 'UTF-8', trim($card["N51_KODAS_KT"]))."\",\"".@iconv('windows-1257', 'UTF-8', str_replace("'", '\\"',str_replace('"', '\"', trim($card["N51_PASTABOS"]))))."\")'>...</button></td></tr>\n";
		}
	}	
}
$from = firstOfPrevMonth();
$to = lastOfPrevMonth();

?>
</table>
</td>
<td valign="top" align="right">
<?php
if (!isset($oldstyle) || $oldstyle != "1") {
?>
<script>
  $(function() {
    $( ".dt" ).datepicker({
      showOn: "button",
      buttonImage: "img/calendar.gif",
      buttonImageOnly: true,
	  dateFormat: "yy.mm.dd",
	  regional: "lt"
    });
  });
  </script>
<?php
}
?>
		<form name="form" method="post" action="printcards.php"><br>	
		<table cellspacing="0" cellpadding="5" class="params">
		<tr><th colspan="2">Ataskaitos</th></tr>
		<tr><td rowspan="2" align="left" valign="top">Pasirinkite periodą:</td><td align="right" nowrap>Nuo <input class="dt" type='text' name="from" value="<?php echo $from;?>"></td></tr>
		<tr><td align="right" nowrap>Iki <input  class="dt" type='text' name="to" value="<?php echo $to;?>"></td></tr>
		<tr><td align="right" colspan="2"><input class="but bgfilter but1 nopad" name="detali" type = "submit" onclick="javascript: form.action='printcards.php';" value = "Detali ataskaita">&nbsp;&nbsp;</td></tr>
		<tr><td align="right" colspan="2"><input class="but bgfilter but1 nopad" name="detali" type = "submit" onclick="javascript: form.action='printcardsgroups.php';" value = "Detali ataskaita pagal prekių grupes">&nbsp;&nbsp;</td></tr>
		<tr><td align="right" colspan="2"><input class="but bgfilter but1 nopad" name="detalicsv" type = "submit" onclick="javascript: form.action='printcards.php';" value = "Detali ataskaita į CSV">&nbsp;&nbsp;</td></tr>
		</table>
		</form>
</td>
</tr></table>
<?php
}
?>
</td></tr>
</table>

<?php
include("footer.php");
?>